package com.dnzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.orm.mybatis.MyBatisBaseServiceImpl;
import org.dnzx.framework.orm.mybatis.MybatisBaseDao;
import org.dnzx.framework.page.PageResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IRoleDao;
import com.dnzx.dao.IUserDao;
import com.dnzx.model.system.RoleBO;
import com.dnzx.model.system.UserBO;
import com.dnzx.security.JWTUserDetails;
import com.dnzx.service.IRoleService;

/**
 * <p>系统后台资源操作service实现类</p>
 * @author 
 * @version V1.0
 * @Package com.dnzx.service.impl
 */
@Service
public class RoleServiceImpl extends MyBatisBaseServiceImpl<RoleBO,Long> implements IRoleService {
	
	@Resource
    private IRoleDao roleDao;
	@Resource
    private IUserDao userDao;
    
    @Override
    public MybatisBaseDao<RoleBO, Long> getBaseDAO() {
        return this.roleDao;
    }

    @Override
    public void save(RoleBO roleBO) throws Exception {
    	int count = this.roleDao.queryCount(roleBO);
    	if (count > 0) {
    		throw new Exception("已存在该名称的角色,请换个名称");
    	}
    	
    	Authentication authen = SecurityContextHolder.getContext().getAuthentication();// 获取当前登录用户
        JWTUserDetails userDetails = (JWTUserDetails)authen.getPrincipal();
    	roleBO.setAddUser(userDetails.getUserId());
        this.roleDao.save(roleBO);
    }
    
    @Override
    public void delete(Long id) throws Exception {
    	UserBO userBO = new UserBO();
    	userBO.setRoleId(id);
    	int count = this.userDao.queryCount(userBO);// 查询该角色下的用户 
    	if (count > 0) {
    		throw new Exception("存在该角色的用户，无法删除。请先删除相关用户");
    	}
        this.roleDao.delete(id);
    }

    @Override
    public void update(RoleBO roleBO) throws Exception {
    	RoleBO roleBODb = this.roleDao.get(roleBO.getId());
    	if (!roleBODb.getName().equals(roleBO.getName())) {// 名称不与原来的一致，则表示名称有更改，需要校验名称是否跟其他重名
    		roleBO.setNameQuery(roleBO.getName());
        	int count = this.roleDao.queryCount(roleBO);
        	if (count > 0) {
        		throw new Exception("已存在该名称的角色,请换个名称");
        	}
    	}
        this.roleDao.update(roleBO);
    }
    
    @Override
    public RoleBO get(Long id) {
    	RoleBO roleBO = this.roleDao.get(id);
    	return roleBO;
    }
    
    @Override
    public List<RoleBO> queryList(RoleBO roleBO) {
    	return this.roleDao.queryList(roleBO);
    }

    @Override
    public PageResult<List<RoleBO>> queryPage(RoleBO roleBO, PageResult<List<RoleBO>> pageResult) {
        int totalRows = this.roleDao.queryCount(roleBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(roleBO.getCurrentPage());
        pageResult.setPageSize(roleBO.getPageSize());
		List<RoleBO> records = this.roleDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), roleBO);
		pageResult.setT(records);
		return pageResult;
    }

}
